package cn.com.fuhuitong.main.mine.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.mine.adapter.CollectionListAdapter
import cn.com.fuhuitong.main.mine.vm.CollectionViewModel
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import kotlinx.android.synthetic.main.activity_mine_collection.*

@Route(path = ARouteConstance.ACTIVITY_URL_MINE_COLLECTION)
class MineCollectionActivity : ViewModeActivity<CollectionViewModel>() {


    private var position: Int = 0

    private lateinit var collectionListAdapter: CollectionListAdapter

    override fun viewModel(): CollectionViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_mine_collection

    override fun initView() {

        image_collection_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(
            recycler_collection_goods, arrayOf(ItemViewType.COLLECTION_LIST)
        )

        delegateAdapter.addAdapter(
            CollectionListAdapter(this, LinearLayoutHelper().apply {
                setDividerHeight(resources.getDimensionPixelSize(R.dimen.dp_10))
            })
                .apply { collectionListAdapter = this }
        )

        collectionListAdapter.setOnItemChildClickListener { view, i, entity ->
            if (view.id == R.id.text_collection_goods_delete) {
                position = i
                viewModel.goodsCollection(entity.id)
            }
        }

    }

    override fun initViewMode() {
        viewModel.collectionListDataLiveData.observe(this, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0,
                emptyContent = EmptyContent("去逛逛", "您还没添加任何商品哦~")
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                if (it.size == 0)
                    return@let
                collectionListAdapter.setAddDataNew(it)
            }
        })

        viewModel.baseDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            collectionListAdapter.removeItemData(position)
            if (collectionListAdapter.data.size == 0) {
                showNullEmptyView(EmptyContent("去逛逛", "您还没添加任何商品哦~"))
            }
        })
    }

    override fun initData() {
        viewModel.goodsCollection()
    }

    override fun onLoadingEmptyView(state: EmptyState) {
        super.onLoadingEmptyView(state)
        viewModel.goodsCollection()
    }
}